package com.education.admin.api.controller;

import com.education.common.annotation.Param;
import com.education.common.annotation.ParamsType;
import com.education.common.annotation.ParamsValidate;
import com.education.common.base.BaseController;
import com.education.common.constants.Constants;
import com.education.common.model.AdminUserSession;
import com.education.common.model.JwtToken;
import com.education.common.utils.*;
import com.education.service.system.SystemAdminService;
import com.education.service.task.LoginSuccessListener;
import com.education.service.task.TaskManager;
import com.education.service.task.TaskParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2021-11-21 00:52
 */
@RestController
public class LoginController extends BaseController {

    @Autowired
    private SystemAdminService systemAdminService;

    @Autowired
    private JwtToken jwtToken;

    @Autowired
    private TaskManager taskManager;

    @PostMapping("/login")
    public Result login(HttpServletRequest request, @RequestBody Map params) {
        String userName = (String) params.get("userName");
        String password = (String) params.get("password");
        String imageCode = (String) params.get("code");
        String key = params.get("key").toString();
        String cacheCode = redisCacheBean.get(key);
        if (ObjectUtils.isEmpty(cacheCode) || !cacheCode.equalsIgnoreCase(imageCode)) {
            return Result.fail(ResultCode.FAIL,"验证码错误");
        }
        Result result = systemAdminService.login(userName, password);
        if (result.isSuccess()) {
            // 获取用户信息
            AdminUserSession adminUserSession = systemAdminService.getAdminUserSession();
            // 加载用户菜单及其权限标记
            systemAdminService.loadUserMenuAndPermission(adminUserSession);
            // 清除map复用
            params.clear();
            // 创建token
            String token = jwtToken.createToken(adminUserSession.getUserMap().get("id"), Constants.FIVE_DAY_TIME_OUT);
            params.put("token", token);
            Map userInfo = new HashMap<>(0);
            userInfo.put("id", adminUserSession.getUserMap().get("id"));
            userInfo.put("menuList", adminUserSession.getMenuList());
            userInfo.put("permissionList", adminUserSession.getPermissionList());
            userInfo.put("login_name", adminUserSession.getUserMap().get("login_name"));
            params.put("userInfo", userInfo);
            result.setData(params);

            // 更新用户登录信息
            TaskParam taskParam = new TaskParam(LoginSuccessListener.class);
            // 获取之前的用户登录次数
            int login_count = (int) adminUserSession.getUserMap().get("login_count");
            taskParam.put("login_count", ++login_count);
            taskParam.put("login_ip", IpUtils.getAddressIp(request));
            taskParam.put("last_login_time", new Date());
            taskParam.put("id", adminUserSession.getUserMap().get("id"));
            taskManager.pushTask(taskParam);
        }
        return result;
    }
}
